/* Create graphs to present experts' beliefs about the different graphing
treatments.
Also rank treatments by expert predicted preferences.
Experts were shown graphs from DGP9 to make their decisions.
*/

clear
version 13.1
set more off
set scheme  s1mono 
					
cd "${main}"

cap log close
log using "${logs}/expert_pred_pref.log", replace

di "This program was last run at: `c(current_time)' `c(current_date)'"

***********************************
* RD
***********************************
* Create pool of experts
use "${dat}/Expert Cornell/Expert Cornell part2.dta", clear
append using "${dat}/Expert Irvine/Expert Irvine part2.dta"
gen seminar = 1
append using "${dat}/Expert Jul 2019 Pilot/Expert Jul 2019 Pilot part2.dta"
append using "${dat}/Expert Aug 2019 Pilot/Expert Aug 2019 Pilot part2.dta"
append using "${dat}/Expert Aug 2019 First Round/Expert Aug 2019 First Round part2.dta"
replace seminar = 0 if seminar != 1
append using "${dat}/Expert Princeton/Expert Princeton part2.dta"
tempfile ex
save "`ex'"
* Merge in their performance
use "${dat}/expert_merged_rd_estimates_micro.dta", clear
collapse (mean) playercorrect, by(pid)
qui sum playercorrect, d 
gen above_median_performance = playercorrect >= r(p50)
drop playercorrect
merge 1:m pid using "`ex'"

foreach s in zero med large {
	foreach c in preferred ne_best ne_worst {

	gen `s'_disc_`c'_gamma = ""
											}
							}
							
foreach s in zero med large {
	foreach c in preferred ne_best ne_worst {
		foreach option in A B C D {

		replace `s'_disc_`c'_gamma = playertreatment_label_`option' if player`s'_disc_`c' == "`option'"
									}		
			
		replace `s'_disc_`c'_gamma = "No impact" if player`s'_disc_`c' == "No impact"
											}
							}	
							
foreach s in zero med large {
	foreach c in preferred ne_best ne_worst {

	tab `s'_disc_`c'_gamma
											}
							}


* Create new variable labels indicating selected treatment for reshape				
foreach s in zero med large {
	foreach c in preferred ne_best ne_worst {

	gen ans_`s'_`c' = ""
											}
							}
							
foreach s in zero med large {
	foreach c in preferred ne_best ne_worst {
		foreach option in A B C D {

		
		replace ans_`s'_`c' = playertreatment_label_`option' if player`s'_disc_`c'  == "`option'"
		
									}		
			
		replace ans_`s'_`c' = "No impact" if player`s'_disc_`c'  == "No impact"
								
		
											}
							}	
	
tempfile rd
save "`rd'", replace

use "`rd'", clear

keep pid ans*

* reshape wide to long twice 
reshape long ans_zero ans_med ans_large, i(pid) j(category) string
reshape long ans, i(pid category) j(size) string
egen cat_size = concat(category size), p(_)
egen count = count(pid), by (cat_size ans)
drop cat_size pid

* relabel variables
replace category = "best" if category == "_ne_best"
replace category = "worst" if category == "_ne_worst"
replace category = "preferred" if category == "_preferred"

replace size = "zero" if size == "_zero"
replace size = "medium" if size == "_med"
replace size = "large" if size == "_large"

* remove duplicates
sort category size ans
quietly by category size ans:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop if ans == "No impact"

* re-order entries 
label define order_size  1 zero  2 medium  3 high  
encode size, gen(size_2) label(order_size)

label define order_cat  1 preferred  2 best  3 worst  
encode category, gen(category_2) label(order_cat)

graph bar count, over(category_2, gap(5)) asyvar ///
                 over(size_2, gap(300))  ///
				 by(ans, note(" ")) ///
				 bar(1, color(red*0.8)) bar(2, color(blue*0.9)) bar(3, color(blue*0.4)) ///
				 ytitle("Count") 
graph export "${output}/expert_sample_rankings_rd_overview.pdf", replace

* ABOVE MEDIAN
use "`rd'", clear
keep if above_median_performance == 1

keep pid ans*

* reshape wide to long twice 
reshape long ans_zero ans_med ans_large, i(pid) j(category) string
reshape long ans, i(pid category) j(size) string
egen cat_size = concat(category size), p(_)
egen count = count(pid), by (cat_size ans)
drop cat_size pid

* relabel variables
replace category = "best" if category == "_ne_best"
replace category = "worst" if category == "_ne_worst"
replace category = "preferred" if category == "_preferred"

replace size = "zero" if size == "_zero"
replace size = "medium" if size == "_med"
replace size = "large" if size == "_large"

* remove duplicates
sort category size ans
quietly by category size ans:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop if ans == "No impact"

* re-order entries 
label define order_size  1 zero  2 medium  3 high  
encode size, gen(size_2) label(order_size)

label define order_cat  1 preferred  2 best  3 worst  
encode category, gen(category_2) label(order_cat)

graph bar count, over(category_2, gap(5)) asyvar ///
                 over(size_2, gap(300))  ///
				 by(ans, note(" ")) ///
				 bar(1, color(red*0.8)) bar(2, color(blue*0.9)) bar(3, color(blue*0.4)) ///
				 ytitle("Count") 
graph export "${output}/expert_sample_rankings_rd_above_median_overview.png", as(png) replace

* BELOW MEDIAN
use "`rd'", clear
keep if above_median_performance == 0

keep pid ans*

* reshape wide to long twice 
reshape long ans_zero ans_med ans_large, i(pid) j(category) string
reshape long ans, i(pid category) j(size) string
egen cat_size = concat(category size), p(_)
egen count = count(pid), by (cat_size ans)
drop cat_size pid

* relabel variables
replace category = "best" if category == "_ne_best"
replace category = "worst" if category == "_ne_worst"
replace category = "preferred" if category == "_preferred"

replace size = "zero" if size == "_zero"
replace size = "medium" if size == "_med"
replace size = "large" if size == "_large"

* remove duplicates
sort category size ans
quietly by category size ans:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop if ans == "No impact"

* re-order entries 
label define order_size  1 zero  2 medium  3 high  
encode size, gen(size_2) label(order_size)

label define order_cat  1 preferred  2 best  3 worst  
encode category, gen(category_2) label(order_cat)

graph bar count, over(category_2, gap(5)) asyvar ///
                 over(size_2, gap(300))  ///
				 by(ans, note(" ")) ///
				 bar(1, color(red*0.8)) bar(2, color(blue*0.9)) bar(3, color(blue*0.4)) ///
				 ytitle("Count") 
graph export "${output}/expert_sample_rankings_rd_below_median_overview.png", as(png) replace

graph close _all

* Compare expert preferences
* +1 point for best, -1 for worst
drop if category == "preferred"
replace count = count * -1 if category == "worst"

preserve
collapse (sum) count, by(ans)
di "overall"
rename count score_total
tempfile rd_scores
save "`rd_scores'"
restore
preserve
keep if size == "large"
collapse (sum) count, by(ans)
rename count score_large
merge 1:1 ans using "`rd_scores'", nogen
save "`rd_scores'", replace
restore
preserve
keep if size == "medium"
collapse (sum) count, by(ans)
rename count score_medium
merge 1:1 ans using "`rd_scores'", nogen
save "`rd_scores'", replace
restore
preserve
keep if size == "zero"
collapse (sum) count, by(ans)
rename count score_zero
merge 1:1 ans using "`rd_scores'", nogen
save "`rd_scores'", replace
save "${dat}/rd_expert_predictions.dta", replace
restore

graph close _all

use "${dat}/rd_expert_predictions.dta", clear
label var ans "Graph method"
label var score_zero "Total points for zero discontinuity (+1 for expert predicting best performance, -1 for worst)"
label var score_medium "Total points for intermediate discontinuity (+1 for expert predicting best performance, -1 for worst)"
label var score_large "Total points for large discontinuity (+1 for expert predicting best performance, -1 for worst)"
save "${dat}/rd_expert_predictions.dta", replace
							
log close
